package db

import (
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"gozero-demo/user/internal/config"
	"time"
)

func NewMysql(c *config.MySqlConfig) sqlx.SqlConn {
	mysql := sqlx.NewMysql(c.DataSource)
	db, err := mysql.RawDB()
	if err != nil {
		panic(err)
	}
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(c.ConnectTimeOut))
	defer cancel()
	if err := db.PingContext(ctx); err != nil {
		panic(err)
	}
	db.SetMaxOpenConns(c.MaxOpenConnects)
	db.SetMaxIdleConns(c.MaxIdleConnects)
	return mysql
}
